<?php
//部门
namespace app\crm\controller;

use app\crm\model\Sys;
use think\Config;
use think\Request;
use think\Exception;
use app\crm\model\Department as DepartmentModel;
use app\crm\model\Staff as StaffModel;
use app\crm\model\Custype as CustypeModel;
class Custype extends \CrmController
{
   public function gettypelist(){
        try {
            $model=new CustypeModel();
            $data=$model->getalltype();
            foreach ($data as $k => $v) {
                # code...
                if($v["seetype"]==1){
                    $data[$k]["seetype_name"]="部分可见";
                }else{
                    $data[$k]["seetype_name"]="全部可见";
                }
            }
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
   }
   public function savenew(Request $request){
        try {
            $model=new CustypeModel();
            $params = delToken($request->get());
            /*type_name:this.classForm.type_name,
                times:this.classForm.longTime,
                seetype_name:this.classForm.can_see_area,
                department_list:"",
                staff_list:""*/
            $data["client_cate_name"]=trim($params["client_cate_name"]);
            $ishas=$model->ishas($data["client_cate_name"],"");
            if($ishas){
                \exception("客户类型已存在");
            }
            $data["times"]=$params["times"];
            if($params["seetype_name"]=="全部可见"){
                $data["seetype"]=0;
                $data["department_list"]="";
                $data["staff_list"]="";
            }
            else{
                $data["department_list"]=$params["department_list"];
                $data["staff_list"]=$params["staff_list"];
                $data["seetype"]=1;
            }
            $data["id"]=$model->addnew($data);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
   }
 
   public function saveupdate(Request $request){
        $model=new CustypeModel();
        try {
            $params = delToken($request->get());

            $data["client_cate_name"]=trim($params["client_cate_name"]);
            $ishas=$model->ishas($data["client_cate_name"],$params["client_cate_id"]);
            if($ishas){
                \exception("客户类型已存在");
            }
            $data["times"]=$params["times"];
            if($params["seetype_name"]=="全部可见"){
                $data["seetype"]=0;
                $data["department_list"]="";
                $data["staff_list"]="";
            }
            else{
                $data["department_list"]=$params["department_list"];
                $data["staff_list"]=$params["staff_list"];
                $data["seetype"]=1;
            }
            $model->updatenew($data,$params["client_cate_id"]);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
   }
   public function deletethis(Request $request){
        $model=new CustypeModel();
        try {
            $params = delToken($request->get());
            $type_id=$params["client_cate_id"];
            $model->deletebyid($type_id);
            $this->successTerminate($params);
        }catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
   }
}
